const getDialogTemplate = (componenetName) => `
<template>
  <el-dialog
    title="任务类型"
    :visible.sync="visible"
    width="400px"
    :close-on-click-modal="false"
    :before-close="handleCancel"
  >
    <el-form
      :model="form"
      :inline="false"
      label-width="100px"
      label-position="right"
      :rules="rules"
      ref="form"
      @submit.prevent.native
    >
      <el-form-item label="备注">
        <el-input v-model="form.remarks" type="textarea"> </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <div class="dialog-btn-panel">
        <el-button @click="handleCancel"><span class="iconfont icon-shanchu2"></span>取消</el-button>
        <el-button type="primary" @click="handleConfirm"><span class="iconfont icon-queren"></span>确定</el-button>
      </div>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: '${componenetName}',
  props: {
    visible: {},
  },

  watch: {
    visible(v) {
      if (v) {
        this.form.remarks = '';
        this.$refs.form && this.$refs.form.resetFields();
      }
    },
  },
  computed: {},
  data() {
    return {
      form: {
        remarks: '',
      },
      rules: {
        // remarks: [
        //   {
        //     required: true,
        //     message: '请输入报废原因',
        //     trigger: 'change',
        //   },
        // ],
      },
    };
  },
  methods: {
    handleCancel() {
      this.$emit('update:visible', false);
    },
    handleConfirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('confirm', this.form);
          this.handleCancel();
        }
      });
    },
  },
};
</script>

<style></style>
`;

const getModifyTemplate = (componenetName) => [
  //template
  {
    repeatReg: new RegExp(`<${componenetName}`),
    targetReg: /(<\/\S*>[\s]*<\/template>[\s]*?<script>)/,
    tagetReplace: `<${componenetName} :visible.sync="dialogShow.${componenetName}" @confirm="saveDialog('${componenetName}', $event)" />\n$1`,
  },
  // import
  {
    repeatReg: new RegExp(
      `import\\s${componenetName}\\sfrom\\s['"']\\.\\/${componenetName}\\.vue['"']`
    ),
    targetReg: /<script>([\s\S]*?)export/,
    tagetReplace: `<script>$1import ${componenetName} from './${componenetName}.vue';

export`,
  },
  // components
  {
    repeatReg: new RegExp(`components:\\s*{[^}]*\\b${componenetName}\\b`),
    checkOptionReg: /components:\s*{/,
    addOptionReg: /(name:.*),/,
    addOptionReplace: `$1,
    components: {
      ${componenetName},
    },`,
    targetReg: /components:\s*{([^}]*)},/,
    tagetReplace: (match, p1) => {
      // 去掉p1中空白字符
      p1 = p1.replace(/\s+/g, "");
      //  如果末尾有，则去掉
      if (p1.endsWith(",")) {
        p1 = p1.slice(0, -1);
      }
      const keys = p1
        .split(",")
        .filter((t) => t !== "\n")
        .filter((t) => !!t);
      keys.push(componenetName);
      return `components:{${keys.join(",")}},`;
    },
  },
  {
    // dialogShow
    repeatReg: new RegExp(`dialogShow:\\s*{[^}]*\\b${componenetName}\\b`),
    checkOptionReg: /dialogShow:\s*{/,
    addOptionReg: /data\(\)\s*{([\s]*?)return\s*{/,
    addOptionReplace: `data\(\) {$1return {
    dialogShow: {
      ${componenetName}: false,
    },`,
    targetReg: /dialogShow:\s*{([^}]*)},/,
    tagetReplace: (match, p1) => {
      // 去掉p1中空白字符
      p1 = p1.replace(/\s+/g, "");
      //  如果末尾有，则去掉
      if (p1.endsWith(",")) {
        p1 = p1.slice(0, -1);
      }
      const keys = p1
        .split(",")
        .filter((t) => t !== "\n")
        .filter((t) => !!t);
      keys.push(`${componenetName}:false`);
      return `dialogShow:{${keys.join(",")}},`;
    },
  },
  {
    // saveDialog
    repeatReg: null,
    checkOptionReg: /saveDialog\(\s*type\s*,\s*data\s*\)/,
    addOptionReg: /methods:\s*{/,
    addOptionReplace: `methods: {
      saveDialog(type, data) {
        switch (type) {
          case '${componenetName}': {
          }
          break;
        }
      },
      showDialog(type) {
        this.dialogShow[type] = true;
      },
      handleClear(type) {
        switch (type) {
          case '${componenetName}': {
            break;
          }
        }
      },`,
    targetReg: /saveDialog\(type, data\) \{\s*switch \(type\) \{/,
    tagetReplace: `saveDialog(type, data) {
      switch (type) {
        case '${componenetName}': {
        }
        break;`,
  },
];
module.exports = {
  getDialogTemplate,
  getModifyTemplate,
};
